package com.jsb.laundry.service.factory.impl;

import com.github.pagehelper.PageHelper;
import com.jsb.laundry.common.enums.UserAddressCity;
import com.jsb.laundry.common.enums.UserAddressCityTown;
import com.jsb.laundry.common.enums.UserAddressRegion;
import com.jsb.laundry.common.response.ResultPage;
import com.jsb.laundry.common.utils.BCUtil;
import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.common.utils.QRCodeUtil;
import com.jsb.laundry.common.utils.aliyun.AliOSSUtil;
import com.jsb.laundry.common.utils.push.WxSendServiceImpl;
import com.jsb.laundry.common.utils.sf.*;
import com.jsb.laundry.dao.factory.LaundryFactoryDao;
import com.jsb.laundry.model.factory.*;
import com.jsb.laundry.service.factory.LaundryFactoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 洗衣工厂模块
 *
 * @data 2023/3/16 13:56
 */
@Service
public class LaundryFactoryServiceImpl implements LaundryFactoryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LaundryFactoryServiceImpl.class);
    @Resource
    private LaundryFactoryDao laundryFactoryDao;

    /**
     * 登录
     *
     * @param sysFactory
     * @return
     */
    @Override
    public int login(SysFactory sysFactory) {
        int login = laundryFactoryDao.login(sysFactory);
        return login;
    }

    @Override
    public List<Site> site() {
        List<Site> site = laundryFactoryDao.site();
        return site;
    }

    @Override
    public List<GoodsInfo> goodsList() {
        return laundryFactoryDao.goodsList();
    }

    @Override
    public List<FactoryOrderInfo> findByOrderInfo(String startTime, String endTime, String goodsId, String keywords) {
        return laundryFactoryDao.findByOrderInfo(startTime, endTime, goodsId, keywords);
    }

    @Override
    public List<FactoryOrderInfo> findByOrderInfos(String startTime, String endTime, String goodsId, String keywords, String payId) {
        return laundryFactoryDao.findByOrderInfos(startTime, endTime, goodsId, keywords, payId);
    }

    /**
     * 入库出库
     *
     * @param orderInfoDtos
     * @return
     */
    @Override
    public Object findruku(OrderInfoDtos orderInfoDtos) {
        //开启分页
        PageHelper.startPage(orderInfoDtos.getPageNum(), orderInfoDtos.getPageSize());
        List<OrderInfoDtos> findruku = laundryFactoryDao.findruku(orderInfoDtos);
        ResultPage<List<OrderInfoDtos>> returnData = new ResultPage<>(findruku);
        return returnData;
    }

    @Override
    public List<OrderinfoList> payIdList(String payId) {
        List<OrderinfoList> orderinfoLists = laundryFactoryDao.payIdList(payId);
        return orderinfoLists;
    }

    @Override
    public int sss(OrderInfoDtos orderInfoDtos) {
        int sss = laundryFactoryDao.sss(orderInfoDtos);
        return sss;
    }

    @Override
    public int ssss(String orderStatus, List<String> payIds) {
        int sss = laundryFactoryDao.ssss(orderStatus, payIds);
        return sss;
    }

    @Override
    public int updInspect(String orderId, String inspect, String flag) {
        return laundryFactoryDao.updInspect(orderId, inspect, flag);
    }

    @Override
    public List<FindByList> findByList(FindByList findByList) {
        return laundryFactoryDao.findByList(findByList);
    }

    @Override
    public int updOrderDetails(FindByList findByList) {
        return laundryFactoryDao.updOrderDetails(findByList);
    }

    @Override
    public OrderInfoShops orderInfoShops1(String orderId, List<String> subOrderId) {
        /**
         * 订单入工厂表 jsb_orders_factory、jsb_orders_factory_item
         */
//        orderEnterFactory(payId);
        orderEnterFactory1(subOrderId);

        OrderInfoShops orderInfoShops = laundryFactoryDao.orderInfoShops1(subOrderId.get(0));
        String riderUploadPic = orderInfoShops.getRiderUploadPic();

        String sendType = orderInfoShops.getSendType();
        if ("1".equals(sendType)) {//小哥顺达
            String tpId = laundryFactoryDao.getFulfillmentsBySubOrderId(subOrderId.get(0));
            if (riderUploadPic != null) {
                List<OrderInfoShops> orderInfoShops3 = laundryFactoryDao.orderInfoShops3(orderId, subOrderId.get(0));
                String zonge = null;
                for (OrderInfoShops infoShops : orderInfoShops3) {
                    zonge = infoShops.getZonge();
                    infoShops.setTpId(tpId);
                }
                orderInfoShops.setOrderIdList(orderInfoShops3);
                orderInfoShops.setTpId(tpId);
//                BufferedImage image = BCUtil.getBarCodeWithWords(subOrderId.get(0), subOrderId.get(0));
                BufferedImage image = BCUtil.getBarCodeWithWords(tpId, tpId);
                MultipartFile multipartFile = BCUtil.convertToMultipartFile(image);
                String uploadFile = AliOSSUtil.uploadFile(multipartFile, "riderUpload");
                orderInfoShops.setTiaoxingma(uploadFile);
                orderInfoShops.setZonge(JSBUtil.turnDoubleStr(zonge));
            }
            List<OrderInfoShops> orderInfoShops3 = laundryFactoryDao.orderInfoShops3(orderId, subOrderId.get(0));
            String zonge = null;
            for (OrderInfoShops infoShops : orderInfoShops3) {
                zonge = infoShops.getZonge();
                infoShops.setTpId(tpId);
            }
            orderInfoShops.setOrderIdList(orderInfoShops3);
//            BufferedImage image = BCUtil.getBarCodeWithWords(subOrderId.get(0), subOrderId.get(0));
            BufferedImage image = BCUtil.getBarCodeWithWords(tpId, tpId);
            MultipartFile multipartFile = BCUtil.convertToMultipartFile(image);
            String uploadFile = AliOSSUtil.uploadFile(multipartFile, "riderUpload");
            orderInfoShops.setTiaoxingma(uploadFile);
            orderInfoShops.setTpId(tpId);
            //生成二维码
            BufferedImage barCodeImg = QRCodeUtil.createImage(tpId);
            MultipartFile multipartFile1 = BCUtil.convertToMultipartFile(barCodeImg);
            //上传至阿里云oss返回url
            String path = AliOSSUtil.uploadFile(multipartFile1, "laundry/qrcode");
            orderInfoShops.setBarCode(path);

            orderInfoShops.setZonge(JSBUtil.turnDoubleStr(zonge));
        } else { //顺丰顺达

            orderInfoShops.getRiderUploadPicList().addAll(new ArrayList<>());
            List<OrderInfoShops> orderInfoShops3 = laundryFactoryDao.orderInfoShops3(orderId, subOrderId.get(0));
            String zonge = null;
            for (OrderInfoShops infoShops : orderInfoShops3) {
                infoShops.setRiderUploadPicList(new ArrayList<>());
                zonge = infoShops.getZonge();
            }
            orderInfoShops.setOrderIdList(orderInfoShops3);
            BufferedImage image = BCUtil.getBarCodeWithWords(orderId, orderId, "", "");
            MultipartFile multipartFile = BCUtil.convertToMultipartFile(image);
            String uploadFile = AliOSSUtil.uploadFile(multipartFile, "SFUpload");
            orderInfoShops.setTiaoxingma(uploadFile);

            orderInfoShops.setZonge(JSBUtil.turnDoubleStr(zonge));
        }

        return orderInfoShops;
    }

    private void orderEnterFactory1(List<String> subOrderIdLs) {
        List<String> subOrderIdLs1 = new ArrayList(new HashSet(subOrderIdLs));
        List<JsbOrdersFactoryItem> factoryItemSubOrder = subOrderIdLs1.stream()
                .map(subOrderId -> new JsbOrdersFactoryItem().setSubOrderId(subOrderId))
                .collect(Collectors.toList());

        /**
         * TODO 主要获取 JsbOrdersItem中 subOrderId、orderId
         */
        List<String> subOrderIdList = factoryItemSubOrder.stream().map(JsbOrdersFactoryItem::getSubOrderId).collect(Collectors.toList());
        List<JsbOrdersItem> subOrderList1 = laundryFactoryDao.getSubOrderItemList(subOrderIdList);
        //让 factoryItemSubOrder 可以拿到orderId
        subOrderList1.forEach(jsbOrdersItem ->
                factoryItemSubOrder.stream()
                        .filter(jsbOrdersFactoryItem ->
                                jsbOrdersFactoryItem.getSubOrderId().equals(jsbOrdersItem.getSubOrderId()))
                        .findFirst()
                        .ifPresent(jsbOrdersFactoryItem ->
                                jsbOrdersFactoryItem.setOrderId(jsbOrdersItem.getOrderId()))
        );
        //主订单id
        List<String> orderIdList = subOrderList1.stream().map(JsbOrdersItem::getOrderId).distinct().collect(Collectors.toList());
        //已入工厂订单主表的订单
        List<JsbOrdersFactory> ordersFactoryItemList = laundryFactoryDao.getOrdersFactoryInfo1(orderIdList);

        //当前还未入工厂子表的订单
        List<JsbOrdersFactoryItem> noInFactoryOrderList = factoryItemSubOrder.stream()
                .filter(item -> ordersFactoryItemList.stream().noneMatch(orderItem -> orderItem.getOrderId().equals(item.getOrderId())))
                .collect(Collectors.toList());

        /**
         * TODO 入工厂表 jsb_orders_factory
         */
        if (!JSBUtil.isEmptyCollection(noInFactoryOrderList)) { //不存在的，则入库

            List<JsbOrders> orderInfo = laundryFactoryDao.getOrderInfo1(orderIdList);

            List<JsbOrdersFactory> ordersFactoryList = orderInfo.stream().map(orderInfoItem -> {
                JsbOrdersFactory ordersFactory = new JsbOrdersFactory();
                ordersFactory.setOrderId(orderInfoItem.getOrderId())
                        .setUserId(orderInfoItem.getUserId())
                        .setShopId(orderInfoItem.getShopId())
                        .setFactoryId(orderInfoItem.getNextToId())
                        .setFactoryOrdersStatus("1")
                        .setType(orderInfoItem.getShippingType());
                return ordersFactory;
            }).collect(Collectors.toList());

            Integer var0 = laundryFactoryDao.addOrderFactory1(ordersFactoryList);
            // 把插入id放入list中后面子表要用
            noInFactoryOrderList.stream()
                    .forEach(jsbOrdersFactoryItem -> ordersFactoryList.stream()
                            .filter(jsbOrdersFactory -> jsbOrdersFactory.getOrderId().equals(jsbOrdersFactoryItem.getOrderId()))
                            .findFirst()
                            .ifPresent(jsbOrdersFactory -> jsbOrdersFactoryItem.setId(jsbOrdersFactory.getId())));

            LOGGER.info("订单入工厂表 jsb_orders_factory：{}", ordersFactoryList);
        }


        //已进入工厂子表的子订单
        List<JsbOrdersFactoryItem> ordersFactoryItems = laundryFactoryDao.getOrdersFactoryItem(factoryItemSubOrder);

        //当前还未入工厂子表的订单
        List<JsbOrdersFactoryItem> noInFactoryOrderItemList = factoryItemSubOrder.stream()
                .filter(item -> ordersFactoryItems.stream().noneMatch(orderItem -> orderItem.getSubOrderId().equals(item.getSubOrderId())))
                .collect(Collectors.toList());

        /**
         * TODO 入工厂表 jsb_orders_factory_item
         */
        if (!JSBUtil.isEmptyCollection(noInFactoryOrderItemList)) { //不存在的订单，则入库

            //父订单入入工厂主表了，当前noInFactoryOrderItemList就没有 id ，无意义的字段考虑移除
            List<JsbOrdersFactoryItem> filteredList = noInFactoryOrderItemList.stream()
                    .filter(item -> item.getId() == null || "".equals(item.getId()))
                    .collect(Collectors.toList());
            if (!JSBUtil.isEmptyCollection(filteredList)) {
                List<JsbOrdersFactory> alreadyIntoFactoryOrder = laundryFactoryDao.getOrdersFactoryInfo1(orderIdList);
                noInFactoryOrderItemList.forEach(jsbOrdersFactoryItem ->
                        alreadyIntoFactoryOrder.stream()
                                .filter(jsbOrdersFactory ->
                                        jsbOrdersFactory.getOrderId().equals(jsbOrdersFactoryItem.getOrderId()))
                                .findFirst()
                                .ifPresent(jsbOrdersFactory ->
                                        jsbOrdersFactoryItem.setId(jsbOrdersFactory.getId()))
                );
            }

            List<String> subOrderIds = noInFactoryOrderItemList.stream().map(JsbOrdersFactoryItem::getSubOrderId).collect(Collectors.toList());

            List<JsbOrdersItem> subOrderList = laundryFactoryDao.getSubOrderItemList(subOrderIds);
            List<JsbOrdersFactoryItem> noInFactoryOrderItemList1 = noInFactoryOrderItemList;
            List<JsbOrdersFactoryItem> jsbOrdersFactoryItem = IntStream.range(0, subOrderList.size())
                    .mapToObj(index -> {
                        JsbOrdersFactoryItem item = new JsbOrdersFactoryItem();
                        JsbOrdersItem jsbOrdersItem = subOrderList.get(index);
                        JsbOrdersFactoryItem noInFactoryOrderItem = noInFactoryOrderItemList1.get(index);

                        item.setOrderFactoryId(noInFactoryOrderItem.getId())
                                .setProductId(jsbOrdersItem.getProductId())
                                .setOrderId(jsbOrdersItem.getOrderId())
                                .setSubOrderId(jsbOrdersItem.getSubOrderId());

                        return item;
                    })
                    .collect(Collectors.toList());

            int var1 = laundryFactoryDao.addOrderFactoryItem(jsbOrdersFactoryItem);
            LOGGER.info("订单入工厂表 jsb_orders_factory_item：{}", jsbOrdersFactoryItem);
        }
    }

    /**
     * 订单入工厂表 jsb_orders_factory、jsb_orders_factory_item
     */
    private void orderEnterFactory(String payId) {
        JsbOrdersFactory jsbOrdersFactory = laundryFactoryDao.getOrdersFactoryInfo(payId);
        if (jsbOrdersFactory == null) { //不存在时，则入库

            /**
             * 入工厂表 jsb_orders_factory
             */
            JsbOrders orderInfo = laundryFactoryDao.getOrderInfo(payId);
            JsbOrdersFactory ordersFactory = new JsbOrdersFactory();
            ordersFactory.setOrderId(payId).setUserId(orderInfo.getUserId()).
                    setFactoryId(orderInfo.getShopId())
                    .setFactoryOrdersStatus("1").setType(orderInfo.getShippingType());
            Integer var0 = laundryFactoryDao.addOrderFactory(ordersFactory);


            /**
             * 入工厂表 jsb_orders_factory_item
             */
            List<JsbOrdersItem> subOrderList = laundryFactoryDao.getSubOrderList(payId);
            List<JsbOrdersFactoryItem> jsbOrdersFactoryItem = subOrderList.stream()
                    .map(jsbOrdersItem -> {
                        JsbOrdersFactoryItem item = new JsbOrdersFactoryItem();
                        item.setOrderFactoryId(var0)
                                .setProductId(jsbOrdersItem.getProductId())
                                .setOrderId(jsbOrdersItem.getOrderId())
                                .setSubOrderId(jsbOrdersItem.getSubOrderId());
                        return item;
                    })
                    .collect(Collectors.toList());

            int var1 = laundryFactoryDao.addOrderFactoryItem(jsbOrdersFactoryItem);
        }
    }


    @Override
    public List<OrderInfoShops> orderInfoShops3(String payId, String subOrderId) {
        return laundryFactoryDao.orderInfoShops3(payId, subOrderId);
    }

    @Override
    public int updImg(String orderId, String postDevelopmentPhotos) {
        return laundryFactoryDao.updImg(orderId, postDevelopmentPhotos);
    }

    @Override
    public void getScanOrderInfo(String payId) {

    }

    @Override
    public int updGoodsId(String goodsId, String serveId, String orderId) {
        return laundryFactoryDao.updGoodsId(goodsId, serveId, orderId);
    }

    /**
     * 全部检查后jsb_orders_factory、jsb_orders有关factory状态变更
     *
     * @param orderId
     * @param picListStr
     * @param flag
     * @return
     */
    @Override
    public int checkClothesEnterOrOut(String orderId, String picListStr, String flag) {
        int var0 = laundryFactoryDao.checkClothesEnterOrOut(orderId, picListStr, flag);
        int var1 = laundryFactoryDao.getCheckIng(orderId, flag);
        /**
         * 全部检查后
         * jsb_orders_factory 中 factory_orders_status 2 已入库 4已出库
         * jsb_orders 中factory_status 1已入库  2已出库
         */
        if (var1 == 0) {
            laundryFactoryDao.updOrderFactoryStatus(orderId, flag);
            laundryFactoryDao.updFactoryOrderStatus(orderId, flag);
        }

//        if (var1 == 0 && "0".equals(flag)) {
//
//        }else if (var1 == 0 && "1".equals(flag)) {
//
//        }
        return var0;
    }


    @Value("${jsb.config.filter.banUser}")
    private List banUser = new ArrayList<>();

    /**
     * 确认出库时
     *
     * @param type
     * @param userId
     * @param payId
     * @param num
     * @return
     */
    @Override
    @Transactional
    public int factoryOrderBack(String type, String userId, String payId, int num, List<String> subOrderId) {
        List<String> subOrderId1 = new ArrayList(new HashSet(subOrderId));
        if ("1".equals(type)) { //0:骑手送达

//            Matcher matcher = Pattern.compile("\\D").matcher(payId);// 匹配非数字字符
//            String orderId = matcher.replaceAll("");
//
//            //微信公众号推送骑手
//            JSONObject time13 = new JSONObject();
//            JSONObject thing2 = new JSONObject();
//            JSONObject character_string8 = new JSONObject();
//            JSONObject object = new JSONObject();
//            object.put("character_string8", character_string8);
//            object.put("time13", time13);
//            character_string8.put("value", orderId);//订单号  int类型
//            time13.put("value", JSBUtil.dateFormat(new Date()));//下单时间
//
//            //获取骑手openid,下单成功给所有表骑手用户发送通知
//            //下单成功给所有表骑手用户发送通知
//            List<WxRiderList> byqOpenId = laundryFactoryDao.findByqOpenId();
//
//            for (WxRiderList wxRiderList : byqOpenId) {
//                WxSendServiceImpl.sendLaundry(object, wxRiderList.getOpenId());
//            }

            //生成返回时的订单id
            String backOrderId = JSBUtil.generateUUID();
//            laundryFactoryDao.updLaundryOrderInfo1(userId, payId, backOrderId);
            laundryFactoryDao.updLaundryOrderInfo1(userId, payId, backOrderId);

            /**
             *  TODO 第二个方向出库时,添加数据到 jsb_orders_driver_item 中
             */
            orderToDriverItem(subOrderId1);

        } else if (banUser.contains(userId)) { //内部人员禁用顺丰下单
            LOGGER.info("内部人员禁用顺丰下单,用户:{},payId:{}", userId, payId);
            return 1;
        } else if ("2".equals(type)) { //1:顺丰送达
            //获取工厂id
            JsbOrders orderInfo = laundryFactoryDao.getOrderInfo(payId);
            String shopsId = orderInfo.getShopId();

            //获取用户填写的上门取衣地址
            List<UserInfoDto> userInfoList = laundryFactoryDao.getLaundryUserDetailInfo(payId, userId);
            UserInfoDto userInfo = userInfoList.get(0);
            //获取工厂信息
            FactoryInfoDto factoryInfo = laundryFactoryDao.getLaundryFactoryInfo(shopsId);
            String city = UserAddressCity.findCityById(factoryInfo.getCityId());
            String town = UserAddressCityTown.findCityById(factoryInfo.getCityTownId());
            String region = UserAddressRegion.findCityById(factoryInfo.getRegionId());
            factoryInfo.setFactoryAddress(city + town + region + factoryInfo.getDetailAddress());

            //获取商品id
            List<String> goodsId = laundryFactoryDao.findOrderIdByPayId(payId);
            //获取子订单信息
            List<JsbOrdersItem> subOrderInfo = laundryFactoryDao.finSubOrderInfo(payId, goodsId);

            //获取是否购买过优惠券
            Boolean buyCouponEd = laundryFactoryDao.buyCouponEd(userId);
            userInfo.setBuyCouponEd(buyCouponEd);
            userInfo.setGoodsId(goodsId.get(0));


            //生成返回时的订单id
            String back_pay_id = JSBUtil.generateUUID();
            //顺丰下单信息
            String orderMsgData = SFMsgDataBuilder.getCreateOrderMsgData(back_pay_id, userInfo, factoryInfo, "1", num);
            //下单返回的信息
            String sfResponse = SFUtil.SFRequest(SFServiceCodeEnum.EXP_RECE_CREATE_ORDER.name(), orderMsgData);
            //解析出来的顺丰单号
            String sf_order_back_id = SFUtil.getSFWaybillNo(sfResponse);

            //支付成功后更新订单的信息 （支付成功后，订单分配给顺丰时更新状态）
            laundryFactoryDao.updLaundryOrderInfo(shopsId, payId, sf_order_back_id, 1, back_pay_id);
            //添加物流记录jsb_orders_fulfillment、jsb_orders_fulfillment_item
            JsbOrdersFulfillment ordersFulfillment = new JsbOrdersFulfillment();
            ordersFulfillment.setOrderId(back_pay_id).setTrackingNumber(sf_order_back_id).setTrackingCompany("顺丰物流")
                    .setTrackingCode("SF").setTrackingDirection("2")
                    .setPickupAddress(userInfo.getPickupAddress()).setDeliveryAddress(userInfo.getUserAddress()).setCustomerName(userInfo.getUserName()).setCustomerTel(userInfo.getUserPhone());

            List<JsbOrdersFulfillmentItem> ordersFulfillmentItem = new ArrayList();
            for (int i = 0; i < goodsId.size(); i++) {
                JsbOrdersFulfillmentItem item = new JsbOrdersFulfillmentItem();
                item.setTrackingNumber(sf_order_back_id).setOrderItemId(subOrderInfo.get(i).getSubOrderId()).setQuantity(1).setProductId(goodsId.get(i));
                ordersFulfillmentItem.add(item);
            }
            laundryFactoryDao.addOrderFulfillment(ordersFulfillment);
            laundryFactoryDao.addOrderFulfillmentItem(ordersFulfillmentItem);


            //推送给用户 订阅号
            WxSend.WxUserSend wxUserSend = new WxSend.WxUserSend();
            Map map = laundryFactoryDao.findUserOrderInfo(userId, payId);
            wxUserSend.setCharacter_string1(payId);
            wxUserSend.setThing2(map.get("goodsTitle").toString());
            if ("1".equals(map.get("sendType").toString())) { //(1:小哥送达，2:顺丰送达)
                wxUserSend.setThing3("聚洗衣专员配送");
                wxUserSend.setCharacter_string4(map.get("qsBackId").toString()); //我们的payId
            } else {
                wxUserSend.setThing3("顺丰配送");
                wxUserSend.setCharacter_string4(map.get("sfBackId").toString()); //顺丰快递单号
            }
            wxUserSend.setTime5(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            wxUserSend.setOpenId(map.get("openId").toString());
            wxUserSend.setTemplateId("WUxYitOWjqFTQn_CQ_RN_2Ye7x-XOIaWgVghIY9K2w8"); //模板id
            WxSendServiceImpl.send(wxUserSend);

        }
        //工厂订单出库填充出库时间
//        laundryFactoryDao.updOrderFactory(payId);


        return 1;
    }

    /**
     * 出库时，第二个方向出库时,添加数据到 jsb_orders_driver_item 中
     *
     * @param subOrderId
     */
    public void orderToDriverItem(List<String> subOrderId) {
        List<JsbOrdersFactoryItem> list = laundryFactoryDao.getOrdersFactoryItemByOrderId(subOrderId);
        List<String> subOrderIdList = list.stream().map(JsbOrdersFactoryItem::getSubOrderId).collect(Collectors.toList());
        List<JsbOrdersItem> ordersItemList = laundryFactoryDao.getShopSubOrderList(subOrderIdList);

        List<com.jsb.laundry.model.shops.JsbOrdersFulfillmentItem> list1 = laundryFactoryDao.getOrdersDriverItem(subOrderIdList);
        ordersItemList.forEach(jsbOrdersItem ->
                list1.stream()
                        .filter(jsbOrdersFulfillmentItem -> jsbOrdersFulfillmentItem.getOrderItemId().equals(jsbOrdersItem.getSubOrderId()))
                        .findFirst()
                        .ifPresent(jsbOrdersFulfillmentItem -> {
                            jsbOrdersItem.setCome(jsbOrdersItem.getNextToId())
                                    .setBack(jsbOrdersItem.getShopId())
                                    .setOrderDirection("2")
                                    .setOrderFulfillmentsId(jsbOrdersFulfillmentItem.getOrderFulfillmentsId()+"A")
                                    .setTrackingNumber(jsbOrdersFulfillmentItem.getTrackingNumber());
                        })
        );

        int var0 = laundryFactoryDao.addOrderToDriverItem(ordersItemList);
        LOGGER.info("工厂已出库订单入司机表jsb_orders_driver_item中:{}", ordersItemList);

    }


    @Override
    public int opColour(String op, String name, String c16, String uuid) {
        if ("0".equals(op)) { //0：添加
            if (JSBUtil.isEmptyString(name) || JSBUtil.isEmptyString(c16)) {
                return 0;
            }
            return laundryFactoryDao.opColour0(name, c16);
        } else if ("1".equals(op)) { //1：修改
            if (JSBUtil.isEmptyString(uuid)) {
                return 0;
            }
            return laundryFactoryDao.opColour1(uuid, name, c16);
        } else if ("2".equals(op)) { //2:删除
            if (JSBUtil.isEmptyString(uuid)) {
                return 0;
            }
            return laundryFactoryDao.opColour2(uuid);
        }
        return 0;
    }

    @Override
    public int opACC(String op, String name, String uuid) {
        if ("0".equals(op)) { //0：添加
            if (JSBUtil.isEmptyString(name)) {
                return 0;
            }
            return laundryFactoryDao.opACC0(name);
        } else if ("1".equals(op)) { //1：修改
            if (JSBUtil.isEmptyString(uuid)) {
                return 0;
            }
            return laundryFactoryDao.opACC1(uuid, name);
        } else if ("2".equals(op)) { //2:删除
            if (JSBUtil.isEmptyString(uuid)) {
                return 0;
            }
            return laundryFactoryDao.opACC2(uuid);
        }
        return 0;
    }

    @Override
    public int opBrand(String op, String name, String pic, String uuid) {
        if ("0".equals(op)) { //0：添加 || JSBUtil.isEmptyString(pic)
            if (JSBUtil.isEmptyString(name)) {
                return 0;
            }
            return laundryFactoryDao.opBrand0(name, pic);
        } else if ("1".equals(op)) { //1：修改
            if (JSBUtil.isEmptyString(uuid)) {
                return 0;
            }
            return laundryFactoryDao.opBrand1(uuid, name, pic);
        } else if ("2".equals(op)) { //2:删除
            if (JSBUtil.isEmptyString(uuid)) {
                return 0;
            }
            return laundryFactoryDao.opBrand2(uuid);
        }
        return 0;
    }

    @Override
    public int opEffect(String op, String name, String uuid) {
        if ("0".equals(op)) { //0：添加
            if (JSBUtil.isEmptyString(name)) {
                return 0;
            }
            return laundryFactoryDao.opEffect0(name);
        } else if ("1".equals(op)) { //1：修改
            if (JSBUtil.isEmptyString(uuid)) {
                return 0;
            }
            return laundryFactoryDao.opEffect1(uuid, name);
        } else if ("2".equals(op)) { //2:删除
            if (JSBUtil.isEmptyString(uuid)) {
                return 0;
            }
            return laundryFactoryDao.opEffect2(uuid);
        }
        return 0;
    }

    @Override
    public int opFlaw(String op, String t, String name, String uuid, String pid) {
        if ("0".equals(op)) { //0：添加
            if (JSBUtil.isEmptyString(name)) {
                return 0;
            }
            return laundryFactoryDao.opFlaw0(t, name, pid);
        } else if ("1".equals(op)) { //1：修改
            if (JSBUtil.isEmptyString(uuid)) {
                return 0;
            }
            return laundryFactoryDao.opFlaw1(t, uuid, pid, name);
        } else if ("2".equals(op)) { //2:删除
            return laundryFactoryDao.opFlaw2(t, uuid, pid);
        }
        return 0;
    }


}
